package com.garen.utils;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;


//工具类
public class FilterUtils {

	//验证URI是否为静态支援
	public static String filterUri(HttpServletRequest httprequest){
		//静态资源直接放过
		String uri = httprequest.getRequestURI();
		String path = httprequest.getContextPath();
		uri = uri.replace(path, "");
		String ctrluri=uri;
		if(uri.indexOf(".") == -1){
			if(uri.indexOf(".do") == -1) {
				//log.debug("--直接放过" + uri);
				return null;
			}
			String method = httprequest.getParameter("method");
			if(method==null) method = "";
			ctrluri = uri.replace(".do","/") + method;
		}
		return ctrluri;
	}
	/*
	 * 获取request请求类型 jsp xml or json 取消xml格式
	 */
	public static String getAcceptType(HttpServletRequest request){
		String type = request.getHeader("accept");
		if(type == null) return "json";
		type = type.toLowerCase();
		if(type.indexOf("json") != -1) return "JSON";
		if(type.indexOf("xml") != -1) return "JSP";
		return "JSON";
	}
	//获取资源大小
	public static int getContentLength(HttpServletRequest httprequest){
		String len = httprequest.getHeader("Content-Length");
		if(len == null) return 0;
		else return Integer.parseInt(len);
	}
		
	///匹配验证
	public static boolean checkURI(List<Pattern> ptnlist,String name){
		for(Pattern p : ptnlist){
			if(p.matcher(name).matches()) 
				return true;
		}
		return false;
	}
		
		
}
